package com.songcha.module_main.ui.activity.h5.tqa

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.webkit.JavascriptInterface
import android.webkit.ValueCallback
import android.webkit.WebView
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.util.DeviceUtil
import com.songcha.library_common.util.PackageUtil
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_webview.ui.activity.WebViewActivity
import org.json.JSONObject

@Route(path = "/main/TQAWebViewActivity")
class TQAWebViewActivity: WebViewActivity() {
    
    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        if(mWebView == null) return
        mWebView!!.addJavascriptInterface(JSObject(this,mWebView!!),"Android")
    }
    
    class JSObject(val activity: Activity, val webView:WebView): Object() {
        @JavascriptInterface
        fun getLoginInfo(){
            loginInfoRet()
        }

        private fun loginInfoRet(){
            Handler(Looper.getMainLooper()).post {
                val jsonObject = JSONObject()
                /*jsonObject.put("packageName",PackageUtil.getPackageName(activity))
                jsonObject.put("channel",PackageUtil.getUmengChannelName(activity))
                jsonObject.put("version",PackageUtil.getPackageVersionName(activity))
                jsonObject.put("token",UserInfoManager.loginToken)
                jsonObject.put("devicePlatform","android")
                jsonObject.put("deviceSystem",Build.VERSION.RELEASE)
                jsonObject.put("deviceBrand",Build.BRAND)
                jsonObject.put("deviceModel",Build.MODEL)
                jsonObject.put("deviceId",DeviceUtil.getAndroidId(activity))*/

                jsonObject.put("packageName", PackageUtil.getPackageName(activity))
                jsonObject.put("channel", PackageUtil.getUmengChannelName(activity))
                jsonObject.put("version", PackageUtil.getPackageVersionName(activity))
                jsonObject.put("isLogin", UserInfoManager.isLogin)
                jsonObject.put("isVip", UserInfoManager.isVip())
                jsonObject.put("token", UserInfoManager.loginToken)
                jsonObject.put("userId", if(UserInfoManager.userInfo != null) UserInfoManager.userInfo!!.id else 0)
                jsonObject.put("devicePlatform","android")
                jsonObject.put("deviceSystem", Build.VERSION.RELEASE)
                jsonObject.put("deviceBrand", Build.BRAND)
                jsonObject.put("deviceModel", Build.MODEL)
                jsonObject.put("deviceId", DeviceUtil.getAndroidId(activity))

                val str = jsonObject.toString()

                webView!!.evaluateJavascript("javascript:loginInfoRet(" + str + ");", ValueCallback {

                })
            }
        }

        @JavascriptInterface
        fun openLink(uriStr:String){
            try{
                val uri = Uri.parse(uriStr)
                val intent = Intent(Intent.ACTION_VIEW, uri)
                activity.startActivity(intent)
            }catch (e:Exception){
                e.printStackTrace()
            }
        }

    }


}
